//display1249.c
#include "display1249.h"


void DIS_PRECOMMAND_1249()
{
   WR_PIN = 0;
   DATA_PIN = 1;      // 1
   
   WR_PIN = 1;
   
   WR_PIN = 0;
   DATA_PIN = 0;      // 0
   
   WR_PIN = 1;
   
   WR_PIN = 0;
   DATA_PIN = 0;      // 0
   
   WR_PIN = 1;
}



void DIS_PREDATA_1249()
{
   WR_PIN = 0;
   DATA_PIN = 1;      // 1
   
   WR_PIN = 1;
   
   WR_PIN = 0;
   DATA_PIN = 0;      // 0
   
   WR_PIN = 1;
   
   WR_PIN = 0;
   DATA_PIN = 1;      // 1
   
   WR_PIN = 1;
}

//-----------------------------------------------
//function :    DIS_INIT_1249
//notice : user must set CS before call this function
//-----------------------------------------------
void DIS_INIT_1249()
{
   
   DIS_PRECOMMAND_1249();
   DIS_COMMAND_1249(C_COMMAND_SYSEN);      //SYSTEM ENABLE
   DIS_COMMAND_1249(C_COMMAND_OPTION_N8);
   
   DIS_COMMAND_1249(C_COMMAND_RC_MASTER_MODE);	// 1st Display Master, Rest slave
   //DIS_COMMAND_1249(C_COMMAND_SLAVE_MODE);  
   
   DIS_COMMAND_1249(C_COMMAND_LEDON);      //LED ON
   DIS_COMMAND_1249(C_COMMAND_BLINKOFF);   //BLINK OFF
   //DIS_COMMAND_1249(C_COMMAND_BLINKON);   //BLINK ON
   DIS_COMMAND_1249(C_COMMAND_PWMDUTY);   //PWM DUTY SET
}


//-----------------------------------------------
//function :    DIS_ALLRAM_1249
//in :       NULL
//out :    NULL
//description:   clear or set all ram
//notice :      user must set CS before call this function
//-----------------------------------------------   
void DIS_ALLRAM_1249(unsigned char F_RAMSET)
{
   unsigned char i;
   
   DIS_PREDATA_1249();            // "101" , ready for write
   DIS_ADDRESS_1249(0);            // address = 0
   
   // all RAM have 96
   for(i=0 ; i<48 ; i++)
   {
      if(F_RAMSET)    DIS_DATA_1249(0xff);   //all RAM write 1
      else         DIS_DATA_1249(0x00);   //all RAM write 0
   }
}




//-----------------------------------------------
//function :    DIS_COMMAND_1249
//in :       command
//out :    NULL
//description:   send command (command-X)
//-----------------------------------------------
void DIS_COMMAND_1249(unsigned char command)
{
   unsigned char i,temp;
      
   temp = 0x80;
   
   for(i=0 ; i<8 ; i++)
   {
      WR_PIN = 0;            //clk = 0 for data ready
      if((temp & command) == 0)    DATA_PIN = 0;
      else            DATA_PIN = 1;
      WR_PIN = 1;            //clk = 1 for data write
      temp >>= 1;
   }
   
   WR_PIN = 0;
   WR_PIN = 1;               // X
}



//-----------------------------------------------
//function :    DIS_ADDRESS_1249
//in :       address
//out :    NULL
//description:   send address(7-bit) for RAM
//-----------------------------------------------
void DIS_ADDRESS_1249(unsigned char address)
{
   unsigned char i,temp;
      
   temp = 0x40;
   
   for(i=0 ; i<7 ; i++)
   {
      WR_PIN = 0;            //clk = 0 for data ready
      if((temp & address) == 0)    DATA_PIN = 0;
      else            DATA_PIN = 1;
      WR_PIN = 1;            //clk = 1 for data write 
      temp >>= 1;
   }
}

//-----------------------------------------------
//function :    DIS_DATA_1249
//in :       data
//out :    NULL
//description:   send data
//-----------------------------------------------
void DIS_DATA_1249(unsigned char vdata)
{
   unsigned char i,temp;
      
   temp = 0x01;
   
   for(i=0 ; i<8 ; i++)
   {
      WR_PIN = 0;            //clk = 0 for data ready
      if((temp & vdata) == 0)    DATA_PIN = 0;
      else         DATA_PIN = 1;
      WR_PIN = 1;            //clk = 1 for data write
      temp <<= 1;
   }
	/*
   temp = 0x80;
   
   for(i=0 ; i<8 ; i++)
   {
      WR_PIN = 0;            //clk = 0 for data ready
      if((temp & vdata) == 0)    DATA_PIN = 0;
      else         DATA_PIN = 1;
      WR_PIN = 1;            //clk = 1 for data write
      temp >>= 1;
   }*/	
}



void display_set(unsigned char adrs, unsigned char vdata)
{
	DIS_PREDATA_1249();			// "101" , ready for write
	DIS_ADDRESS_1249(adrs*2);	
	DIS_DATA_1249(vdata);
}